package mobileann.mafamily.utils;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.mofind.android.base.L;
import com.nostra13.universalimageloader.core.assist.ContentLengthInputStream;

import mobileann.mafamily.act.eye.SafeSettingActivity;
import mobileann.mafamily.act.main.FS;
import mobileann.mafamily.act.setup.HelpActivityDetail;
import mobileann.mafamily.lockscreen.LockSettingActivity;
import mobileann.mafamily.lockscreen.SetGuideActivity;
import mobileann.mafamily.receiver.StartApplicationReceiver;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.AppOpsManager;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.widget.Toast;

/**
 * 功能:旨在将设置类的方法放在这里,作为公共用
 * <li>小米手机调用适配</li>
 * <li>乐视手机调用适配</li>
 * <li>华为手机手机调用适配</li>
 * @author Adrian.20151112
 */
public class SystemUtils {

	public static final String NORMAL_INFORMATION = "开启消息通知";
	public static final String NORMAL_SYSTEMLOCKSCREEN = "关闭系统锁屏";
	public static final String NORMAL_WHITELIST = "白名单设置";
	// 小米
	public static final String MIUI_OPENFLOATINGWINDOW = "小米权限设置";
	public static final String MIUI_OPENTRUSTAPP = "设置信任列表自动启动";
	// 乐视
	public static final String LETV_LAUNCHBOOT = "开机自启动";
	public static final String LETV_PERMISSIONS = "权限设置";
	// 有权查看使用的情况
	public static final String L_USAGESTATE = "有权限查看应用详情的应用";
	// 华为
	public static final String HUAWEI_APPPROTECT = "受保护的应用";
	public static final String HUAWEI_USAGESTATE = L_USAGESTATE;
	public static final String HUAWEI_PERMISSIONS = "权限管理";
	// 魅族
	//* <li>1、应用权限设置 </li>
	// * <li>3、省电模式 </li>
	//* <li>4    安全中心—应用清理 </li>
	public static final String MEIZU_APPPERMISSION = "应用权限管理";
	public static final String MEIZU_POWERSAVING = "省电模式";
	public static final String MEIZU_CLEANAPP = "安全中心-应用清理";
	// samsung适配
	public static final String SAMSUNG_LAUNCHBOOT = "开机自启动";
	public static final String SAMSUNG_POWERSAVING = "省电模式";
	// vivo
	public static final String VIVO_LAUNCHBOOT= "开机自启动";
	public static final String VIVO_APPPERMISSION = "权限设置";
	public static final String VIVO_POWERSAVING = "省电管理";

	private static SystemUtils instance = null;

	/** 获取MIUI的版本 */
	private String miuiVersion = null;
	/** 是否是MIUI */
	private boolean IS_MIUI = false;
	private boolean IS_LETV = false;
	private boolean IS_HUAWEI = false;
	private boolean IS_MEIZU = false;
	private boolean IS_SAMSUNG = false;
	private boolean IS_VIVO = false;

	public static SystemUtils getInstance() {
		if (instance == null) {
			instance = new SystemUtils();
		}
		return instance;
	}

	private SystemUtils(){
		if(StartDetectionUtils.isMIUI()){
			IS_MIUI = true;
			miuiVersion = StartDetectionUtils.getMIUIVersion();
		} else if(StartDetectionUtils.isLETV()) {
			IS_LETV = true;
		} else if(StartDetectionUtils.isHUAWEI()){
			IS_HUAWEI = true;
		} else if(StartDetectionUtils.isMEIZU()){
			IS_MEIZU = true;
		} else if(StartDetectionUtils.isSAMSUNG()){
			IS_SAMSUNG = true;
		} else if(StartDetectionUtils.isVIVO()) { 
			IS_VIVO = true;
		} else { //为了保险重新设置一下为false
			IS_MIUI = false;
			IS_LETV = false;
			IS_HUAWEI = false;
			IS_MEIZU = false;
			IS_SAMSUNG = false;
			IS_VIVO = false;
		}
	}

	public boolean isMIUI(){
		return IS_MIUI;
	}

	public boolean isLETV(){
		return IS_LETV;
	}

	public boolean isHUAWEI(){
		return IS_HUAWEI;
	}


	public boolean isMEIZU(){
		return IS_MEIZU;
	}

	public boolean isSAMSUNG(){
		return IS_SAMSUNG;
	}

	public boolean isVIVO(){
		return IS_VIVO;
	}

	/**
	 * 功能: 跳转到新的Activity
	 * @param ctx
	 * @param type
	 */
	private void startGuideView(Context ctx, int type){
		Intent toSet = new Intent(ctx, SetGuideActivity.class);
		toSet.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		toSet.putExtra("ViewType", type);
		ctx.startActivity(toSet);
	}
	
	private void startGuideView(Context ctx, int type, Intent intentToSet) {
		Intent toSet = new Intent(ctx, SetGuideActivity.class);
		toSet.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		toSet.putExtra("ViewType", type);
		if (intentToSet != null) {
			toSet.putExtra("isIncludeIntent", true);
			toSet.putExtra("intent", intentToSet);
		}
		L.i("startGuideView");
		ctx.startActivity(toSet);
	}


	/**
	 * 主要功能:
	 * <br>开启消息通知
	 * @annotation ADRIAN.2015.8.4.18.41
	 */
	public synchronized void goNotifySetting(Context ctx){
		int sdk_int = Build.VERSION.SDK_INT;
		if(sdk_int >= 19){
			Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
			ctx.startActivity(intent);
			startGuideView(ctx, SetGuideActivity.TYPE_GO_NTF_SETTING_19);
		}else{
			Intent intent =  new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);  
			ctx.startActivity(intent);
			startGuideView(ctx, SetGuideActivity.TYPE_GO_NTF_SETTING_BELOW);
		}
	}

	/**
	 * 主要功能:
	 * <br>关闭系统锁屏设置
	 * @annotation ADRIAN.2015.8.4.18.47
	 */
	public void goLockSetting(Context ctx){
		if(IS_MIUI){
			// 跳转开发人员选项界面
			Intent intent =  new Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);  
			ctx.startActivity(intent);
			startGuideView(ctx, SetGuideActivity.TYPE_GO_LOCK_SETTING_MI);
		}else{
			try {
				Intent intent = new Intent("/");
				ComponentName cm = new ComponentName("com.android.settings","com.android.settings.ChooseLockGeneric");
				intent.setComponent(cm);
				//				ctx.startActivityForResult(intent, 0);
				ctx.startActivity(intent);
				startGuideView(ctx, SetGuideActivity.TYPE_GO_LOCK_SETTING_OTHER);
			} catch (Exception e) {
			}
		}
	}
	
	public void goHideMode(Context ctx){
		if(IS_MIUI){
			try {
				Intent itokeeper = new Intent();
				ComponentName cm = new ComponentName("com.miui.powerkeeper",
						"com.miui.poweroptimize.hidemode.PowerHideModeActivity");
				itokeeper.setComponent(cm);
				itokeeper.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				ctx.startActivity(itokeeper);
			} catch (Exception e) {
				// TODO: handle exception
				//com.miui.powerkeeper.ui.PowerHideModeActivity
				Intent itokeeper2 = new Intent();
				ComponentName cm = new ComponentName("com.miui.powerkeeper",
						"com.miui.powerkeeper.ui.PowerHideModeActivity");
				itokeeper2.setComponent(cm);
				itokeeper2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				ctx.startActivity(itokeeper2);

			}
		}
	}

	//	/**  */
	//	public synchronized void AskPermissionOnL() {
	//		if (Build.VERSION.SDK_INT >= 21) {
	//			doAskPermissionOnL();
	//		}
	//	}
	//	
	//	@TargetApi(21)
	//	private void doAskPermissionOnL() {
	//		if (needPermissionForBlocking(FS.getInstance())) {
	//			Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
	//			if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0)
	//				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	//			try {
	//				FS.getInstance().startActivity(intent);
	//			} catch (Exception e) {
	//				//handle exception
	//			}
	//		}
	//	}
	//	
	//	@TargetApi(21)
	//	private boolean needPermissionForBlocking(Context context) {
	//		try {
	//			PackageManager packageManager = context.getPackageManager();
	//			ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
	//			AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
	//			int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid,
	//					applicationInfo.packageName);
	//			return (mode != AppOpsManager.MODE_ALLOWED);
	//		} catch (PackageManager.NameNotFoundException e) {
	//			return true;
	//		}
	//	}

	/**
	 * 查看有权访问权限是否打开
	 * <li></li>
	 * @return
	 */
	@SuppressLint("NewApi")
	public boolean hasUsageAccess(Context ctx) {
		if (Build.VERSION.SDK_INT >= 21){ 
			PackageManager packageManager = ctx.getPackageManager();
			Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
			List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
					PackageManager.MATCH_DEFAULT_ONLY);
			return list.size() > 0;
		} else {
			return true; // 如果版本较低, 就不要显示了
		}
	}

	/**
	 *  the user enabled my application
	 *  <li>有权查看使用的情况---这个好用一些</li>
	 * @return
	 */
	@SuppressLint("NewApi")
	public boolean hasEnable(Context ctx){
		if (Build.VERSION.SDK_INT >= 21){
			long ts = System.currentTimeMillis();
			UsageStatsManager usageStatsManager=(UsageStatsManager)ctx.getApplicationContext().getSystemService("usagestats");
			List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST,0, ts);
			if (queryUsageStats == null || queryUsageStats.isEmpty()) {
				return false;
			}
		} 
		return true;
	}

	/**
	 * 询问是否有<有权访问使用的情况>权限,如果没有则去设置
	 * @param ctx
	 */
	@TargetApi(21)
	private boolean doAskPermissionOnL(Context ctx) {
		if (hasUsageAccess(ctx)) {
			Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
			// if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0)
			// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			try {
				ctx.startActivity(intent);
				startGuideView(ctx, SetGuideActivity.TYPE_GO_LOCK_USAGESTATES);
			} catch (Exception e) {
				//handle exception
			}
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 如果SDK>=21就会去这是, 如果不大于就返回false
	 * @param ctx
	 * @return
	 */
	public synchronized boolean AskPermissionOnL(Context ctx) {
		if (Build.VERSION.SDK_INT >= 21) {
			return doAskPermissionOnL(ctx);
		}  else {
			return false;
		}
	}

	/**
	 * 专为乐视手机的跳转到管家界面
	 * @param ctx
	 * @return
	 */
	public synchronized boolean gotLetvLaunchBoot(Context ctx) {
		//PackageManager packageManager = ctx.getPackageManager();
		Intent intent=ctx.getPackageManager().getLaunchIntentForPackage("com.letv.android.supermanager");
		ctx.startActivity(intent);
		return true;
	}



	/**
	 * 白名单设置
	 * @param ctx
	 * @return
	 */
	public synchronized boolean goWhiteList(Context ctx){
		if(ListSettingUtils.getInstance().isShowSafeApp(ctx)){
			Intent intent = new Intent(ctx, SafeSettingActivity.class);
			ctx.startActivity(intent);
			return true;
		}/*else{
			Toast.makeText(ctx, "您不需要设置", Toast.LENGTH_SHORT).show();
		}*/
		return false;
	}

	/** 获取栈顶的Activity名字 */
	public String getTopActivity(Context ctx) {
		ActivityManager manager = (ActivityManager) ctx.getSystemService(ctx.ACTIVITY_SERVICE);
		List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
		if (runningTaskInfos != null) {
			return runningTaskInfos.get(0).baseActivity.getPackageName();
			//			return (runningTaskInfos.get(0).topActivity).toString();
		} else {
			return null;
		}
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//TODO~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//@@@普通手机专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	/**
	 * 跳转到系统设置页面
	 * @param ctx
	 */
	public boolean startSystemSetting(Context ctx){
		try {
			Intent intent =  new Intent(Settings.ACTION_SETTINGS);  
			ctx.startActivity(intent);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	//@@@普通手机专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//TODO~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//@@@小米手机专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	/** 获取MIUI的版本号 */
	public void getMIUIversion() {
		//TODO
	}
	
	
	/**
	 * 主要功能:
	 * <br>兼容MIUI5和MIUI6的,开启悬浮窗设置界面
	 * @annotation ADRIAN.2015.8.4.18.37
	 */
	public boolean goStartSetting(Context ctx){
		if(IS_MIUI){
			if("V6".equalsIgnoreCase(miuiVersion) || "V7".equalsIgnoreCase(miuiVersion)){
				Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
				intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");  
				intent.putExtra("extra_pkgname", FS.getInstance().getPackageName());
				ctx.startActivity(intent);
				startGuideView(ctx, SetGuideActivity.TYPE_GO_START_SETTING_V6);
				//	Intent intent = new Intent(
				//	Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
				//	Uri uri = Uri.fromParts("package", FS.getInstance().getPackageName(), null);
				//	intent.setData(uri);
			}else{
				String pkgName = StartDetectionUtils.getPkgName(ctx);
				Uri packageURI = Uri.parse("package:" + pkgName);
				Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
				ctx.startActivity(intent);
				startGuideView(ctx, SetGuideActivity.TYPE_GO_START_SETTING_V5);
			} 
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 主要功能:
	 * <br> 设置自启动管理,主要适用小米手机,其它未考虑
	 * <br> 步骤
	 * <br> 1. 先判断是不是MIUI
	 * <br> 2. 
	 * @annotation ADRIAN.2015.8.4.18.40
	 */
	public synchronized boolean goTrustList(Context ctx){
		if(IS_MIUI){
			if("V6".equalsIgnoreCase(miuiVersion)||"V7".equalsIgnoreCase(miuiVersion)){
				try{
					Intent localIntent = new Intent();
					localIntent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
					ctx.startActivity(localIntent);
					startGuideView(ctx, SetGuideActivity.TYPE_GO_TRUSTLIST_V6);
					return true;
				}catch (Exception localException){
				}
			}else{
				PackageManager pm = ctx.getPackageManager();
				PackageInfo info = null;
				try {
					info = pm.getPackageInfo(FS.getInstance().getPackageName(), 0);
				} catch (NameNotFoundException e) {
					e.printStackTrace();
				}
				Intent i = new Intent("miui.intent.action.APP_PERM_EDITOR");
				i.setClassName("com.android.settings", "com.miui.securitycenter.permission.AppPermissionsEditor");
				i.putExtra("extra_package_uid", info.applicationInfo.uid);
				try {
					ctx.startActivity(i);
					startGuideView(ctx, SetGuideActivity.TYPE_GO_TRUSTLIST_V5);
				} catch (Exception e) {
					//	Toast.makeText(this, "只有MIUI才可以设置哦", Toast.LENGTH_SHORT).show();
				}
			}
			return true;
		} else {
			return false;
		}
	}
	
	/** 跳转到小米的安全中心里面 */
	public boolean startMIUISecurity(Context ctx) {
		// com.miui.securitycenter.ui.settings.SettingsActivity
		// com.miui.securitycenter
		// miui.intent.action.APP_SETTINGS
		//------------
		// com.miui.securitycenter.settings.SettingsActivity
		if (IS_MIUI) {
			try {
				Intent i = new Intent();
				i.setClassName("com.miui.securitycenter", "com.miui.securitycenter.ui.settings.SettingsActivity");
				i.setAction("miui.intent.action.APP_SETTINGS");
				ctx.startActivity(i);
			} catch (Exception e) {
				try {
					Intent i = new Intent();
					i.setClassName("com.miui.securitycenter", "com.miui.securitycenter.settings.SettingsActivity");
					i.setAction("miui.intent.action.APP_SETTINGS");
					ctx.startActivity(i);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
			return true;
		} else {
			return false;
		}
	}

	//@@@小米手机专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//TODO~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//@@@乐视手机专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	/**
	 * 适配乐视手机, 跳转到自启动
	 * @param ctx
	 */
	public boolean startPermissionAutoBootActivity(Context ctx) {
		boolean hasLetvsafe = false;
		try {
			ApplicationInfo info = ctx.getPackageManager().getApplicationInfo("com.letv.android.letvsafe",
					PackageManager.GET_UNINSTALLED_PACKAGES);
			hasLetvsafe = info != null;
		} catch (PackageManager.NameNotFoundException e) {
			hasLetvsafe = false;
		}
		if (hasLetvsafe) {
			String ACTION_PERMISSION_AUTOBOOT = "com.letv.android.permissionautoboot";
			Intent intent = new Intent(ACTION_PERMISSION_AUTOBOOT);
			// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |
			// Intent.FLAG_ACTIVITY_NEW_TASK);
			ctx.startActivity(intent);
			startGuideView(ctx, SetGuideActivity.TYPE_GO_LOCK_TRUSTLIST_LETV);
			return true;
		} else {
			return false;
		}
	}


	/***
	 * 适配乐视手机, 跳转到自启动
	 * @param ctx
	 */
	public void startPermissionAppPermission(Context ctx) {
		boolean hasLetvsafe = false;
		try {
			ApplicationInfo info = ctx.getPackageManager().getApplicationInfo("com.letv.android.letvsafe",
					PackageManager.GET_UNINSTALLED_PACKAGES);
			hasLetvsafe = info != null;
		} catch (PackageManager.NameNotFoundException e) {
			hasLetvsafe = false;
		}
		if (hasLetvsafe) {
			String ACTION_PERMISSION_AUTOBOOT = "com.letv.android.permissionandapps";
			Intent intent = new Intent(ACTION_PERMISSION_AUTOBOOT);
			// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |
			// Intent.FLAG_ACTIVITY_NEW_TASK);
			ctx.startActivity(intent);
			startGuideView(ctx, SetGuideActivity.TYPE_GO_LOCK_SETTING_LETV);
		}
	}

	//@@@乐视手机专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//TODO~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//@@@华为手机专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	//	try {
	//        // 华为大坑，不能直接用Intent来启动，会启不起来
	//        String cmd = "am start -n com.huawei.systemmanager/.optimize.process.ProtectActivity";
	//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
	//            cmd += " --user " + getUserSerial();
	//        }
	//        Runtime.getRuntime().exec(cmd);
	//    } catch (IOException ex) {
	//        LogWrapper.e(TAG, ex);
	//    }
	//    // 先获取上下文，避免跳转到其它界面后Fragment与Activity解绑，getActivity()空指针
	//    final Context context = getActivity();
	//    mHandler.postDelayed(new Runnable() {
	//        @Override
	//        public void run() {
	//            Intent intent = new Intent(context, ProtectedActivity.class);
	//            intent.putExtra(ErtaoConstants.EXTRA_FRAGMENT_CLASS, ProtectedFragment.class.getName());
	//            intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
	//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	//            context.startActivity(intent);
	//        }
	//    }, 1000);

	/**
	 * 适配华为手机---->>>通过命令打开受保护的应用<<<
	 * @param ctx
	 */
	public void startProtectApp(Context ctx){
		try {
			// 华为大坑，不能直接用Intent来启动，会启不起来
			String cmd = "am start -n com.huawei.systemmanager/.optimize.process.ProtectActivity";
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
				cmd += " --user " + getUserSerial(ctx);
			}
			Runtime.getRuntime().exec(cmd);
		} catch (IOException ex) {
		}

		//		Intent intent = new Intent(ctx, ProtectedActivity.class);
		//		intent.putExtra(ErtaoConstants.EXTRA_FRAGMENT_CLASS, ProtectedFragment.class.getName());
		//		intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
		//		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		//		context.startActivity(intent);
		// 先获取上下文，避免跳转到其它界面后Fragment与Activity解绑，getActivity()空指针
		//		final Context context = getActivity();
		//		mHandler.postDelayed(new Runnable() {
		//			@Override
		//			public void run() {
		//				Intent intent = new Intent(context, ProtectedActivity.class);
		//				intent.putExtra(ErtaoConstants.EXTRA_FRAGMENT_CLASS, ProtectedFragment.class.getName());
		//				intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
		//				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		//				context.startActivity(intent);
		//			}
		//		}, 1000);
	}

	/**
	 * 通过反射获得用户权限的序列号
	 * @param ctx
	 * @return
	 */
	public String getUserSerial(Context ctx) {
		//noinspection ResourceType
		Object userManager = ctx.getSystemService("user");
		if (userManager == null) return "";

		try {
			Method myUserHandleMethod = android.os.Process.class.getMethod("myUserHandle", (Class<?>[]) null);
			Object myUserHandle = myUserHandleMethod.invoke(android.os.Process.class, (Object[]) null);
			Method getSerialNumberForUser = userManager.getClass().getMethod("getSerialNumberForUser", myUserHandle.getClass());
			long userSerial = (Long) getSerialNumberForUser.invoke(userManager, myUserHandle);
			return String.valueOf(userSerial);
		} catch (NoSuchMethodException ex) {
		} catch (IllegalArgumentException ex) {
		} catch (IllegalAccessException ex) {
		} catch (InvocationTargetException ex) {
		}
		return "";
	}


	public void startPermission(Context ctx) {
		//com.huawei.permissionmanager.ui.MainActivity
		//com.huawei.permissionmanager.ui.SingleAppActivity
		// com.android.permission.system_manager_interface
		Intent intent = new Intent("/");
		ComponentName cn = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");
		intent.setComponent(cn);
		//		String pkgName = StartDetectionUtils.getPkgName(ctx);
		//		Uri packageURI = Uri.parse("package:" + pkgName);
		//		intent.setData(packageURI);
		ctx.startActivity(intent);

		//		String pkgName = StartDetectionUtils.getPkgName(this);
		//		Uri packageURI = Uri.parse("package:" + pkgName);
		//		Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
		//		startActivity(intent);
	}
	
	public boolean startHUAWEISystemManager(Context ctx){
		//com.huawei.systemmanager
		//TODO
		try {
			Intent intent=ctx.getPackageManager().getLaunchIntentForPackage("com.huawei.systemmanager");
			ctx.startActivity(intent);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	//@@@华为手机专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//@@@魅族手机专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	public synchronized boolean gotMeiLaunchSecurity(Context ctx) {
		//PackageManager packageManager = ctx.getPackageManager();
		Intent intent=ctx.getPackageManager().getLaunchIntentForPackage("com.meizu.safe");
		ctx.startActivity(intent);
		return true;
	}


	//@@@魅族手机专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	public boolean startMEIZUPermission(Context ctx){
		try {
			Intent i = new Intent();
			i.setClassName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity");
			i.putExtra("packageName", "com.mobileann.mafamily");
			ctx.startActivity(i);
			startGuideView(ctx, SetGuideActivity.TYPE_GO_LOCK_MEIZU_PERMISSION);
			return true;
		} catch (Exception e) {
			return false;
		}
		//		try {
		//			// am start -n com.meizu.safe/.security.AppSecActivity -e 'packageName' 'com.mobileann.mafamily' --user 0
		//			String cmd = "adb shell am start -n com.meizu.safe/.security.AppSecActivity -e 'packageName' com.mobileann.mafamily ";
		//			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
		//				cmd += " --user " + getUserSerial(ctx);
		//			}
		//			Runtime.getRuntime().exec(cmd);
		//		} catch (IOException ex) {
		//			ex.printStackTrace();
		//		}
	}

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//@@@三星手机测试util专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	/**
	 *  Application Permmission 未适配成功
	 * @param ctx
	 * @return
	 */
	public boolean startSamsungAppPermmission(Context ctx){
		//		try {
		//			Intent i = new Intent();
		//			i.setClassName("com.sec.android.app.capabilitymanager", "com.sec.android.app.cm.ui.CMPermissionGroupListActivity");
		//			i.putExtra("packageName", "com.mobileann.mafamily");
		//			ctx.startActivity(i);
		//			startGuideView(ctx, SetGuideActivity.TYPE_GO_LOCK_MEIZU_PERMISSION);
		//			return true;
		//		} catch (Exception e) {
		//			e.printStackTrace();
		//		}
		//com.sec.android.app.cm.ui.CMPermissionGroupListActivity
		try {
			Intent intent =  new Intent(Settings.ACTION_SETTINGS);  
//			ctx.startActivity(intent);
			startGuideView(ctx, SetGuideActivity.TYPE_GO_LOCK_SAMSUNG_LAUNCHBOOT, intent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 省电模式  未适配成功
	 * @param ctx
	 * @return
	 */
	public boolean startSamsungSystemSetting(Context ctx){
		try {
			Intent intent =  new Intent(Settings.ACTION_SETTINGS);  
//			ctx.startActivity(intent);
			startGuideView(ctx, SetGuideActivity.TYPE_GO_LOCK_SAMSUNG_POWERSAVING,intent);
			//			Intent intent = new Intent();
			//			ComponentName cn = new ComponentName("com.android.setting","com.android.subsetting");
			//			intent.setComponent(cn);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	//@@@三星手机测试util专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//@@@VIVO手机测试util专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//12-01 17:03:02.096: I/maf(23453): ===>>>com.iqoo.secure

	/**
	 * vivo开机自启动 
	 * @param ctx
	 * @return
	 */
	public boolean startVIVOLanuchboot(Context ctx){ 
		Intent intent=ctx.getPackageManager().getLaunchIntentForPackage("com.iqoo.secure");
//		ctx.startActivity(intent);
		startGuideView(ctx, SetGuideActivity.TYPE_GO_LOCK_VIVO_LANCHBOOT, intent);
		return false;
	}

	public boolean startVIVOAppPermission(final Activity ctx){
		Intent intent=ctx.getPackageManager().getLaunchIntentForPackage("com.iqoo.secure");
//		ctx.startActivity(intent);
		startGuideView(ctx, SetGuideActivity.TYPE_GO_LOCK_VIVO_PERMISSION, intent);				
		return false;
	}

	public boolean startVIVOPowersaving(Context ctx){
		Intent intent=ctx.getPackageManager().getLaunchIntentForPackage("com.iqoo.secure");
//		ctx.startActivity(intent);
		startGuideView(ctx, SetGuideActivity.TYPE_GO_LOCK_VIVO_POWERSAVING, intent);
		return false;
	}

	//@@@VIVO手机测试util专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//@@@手机测试util专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	public static String getCurrentActivityName(Context context) {
		ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);


		// get the info from the currently running task
		List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);

		ComponentName componentInfo = taskInfo.get(0).topActivity;
		return componentInfo.getClassName();
	}


	//@@@手机测试util专区@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


}
